package com.guoby.controller.login;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import java.util.Date;

/**
 * Created by pactera on 2018/3/9.
 */
@RestController
public class LoginController {

    @PostMapping("/login")
    public String login(@RequestParam("username") String name,
                        @RequestParam("password") String pass) throws ServletException {

        String token = "";
        if (!"admin".equals(name)) {
            throw new ServletException("找不到用户");
        }

        if (!"123456".equals(pass)) {
            throw new ServletException("密码有误");
        }

        token = Jwts.builder().setSubject(name).claim("role", "user").setIssuedAt(new Date()).signWith(SignatureAlgorithm.HS256, "base64EncodedSecretKey").compact();
        return token;
    }

}
